package com.xzk.ExpressInnWeb.controller.express;

import com.xzk.ExpressInnWeb.bean.BootstrapTableExpress;
import com.xzk.ExpressInnWeb.bean.Express;
import com.xzk.ExpressInnWeb.bean.Message;
import com.xzk.ExpressInnWeb.bean.ResultData;
import com.xzk.ExpressInnWeb.exception.SmsServiceNotAvailableException;
import com.xzk.ExpressInnWeb.service.ExpressService;
import com.xzk.ExpressInnWeb.util.ExUtil;
import com.xzk.ExpressInnWeb.util.JsonUtil;
import com.xzk.ExpressInnWeb.util.AdminUtil;
import com.xzk.ExpressInnWeb.web.mvc.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class ExpressController {

    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = ExpressService.consoleEx();
        Message msg = new Message();
        if (data.size() == 0) {
            // 查询失败
            msg.setStatus(-1);
        } else {
            // 查询成功
            msg.setStatus(0);
        }
        // 只是在页面上展示，无需返回结果
        // 所以不需要msg.setResult()
        msg.setData(data);
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        // 1. 接收前端传递的数据，如第几页，每页有多少数据
        // 1.1  获取起始偏移值(获取查询数据的起始索引值)，即从第几条开始查询(offset)
        String offsetStr = req.getParameter("offset");
        int offset = Integer.parseInt(offsetStr);
        // 1.2  获取当前页需要查询的数据条数
        String exSizeStr = req.getParameter("exSize");
        int exSize = Integer.parseInt(exSizeStr);
        // 2. 进行分页查询
        // 2.1  查询所有快递
        List<BootstrapTableExpress> list
                = ExUtil.transferExList(
                        ExpressService.findAllEx(true, offset, exSize));
        // 2.2  查询当前快递的总数
        List<Map<String, Integer>> all = ExpressService.consoleEx();
        int total = all.get(0).get("data1_size");
        // 3. 将集合封装为 bootstrap-table 识别的格式
        ResultData<BootstrapTableExpress> data = new ResultData<>();
        data.setRows(list);
        data.setTotal(total);
        // 4. 将封装好的 ResultData 对象 转换为JSON并返回
        return JsonUtil.toJSON(data);
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String ex_num = req.getParameter("ex_num");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userphone = req.getParameter("userphone");
        String staffphone = req.getParameter("staffphone");
        Express ex = new Express();
        ex.setEx_num(ex_num);
        ex.setCompany(company);
        ex.setUsername(username);
        ex.setUserphone(userphone);
        if (staffphone!=null) {
            ex.setStaffphone(staffphone);
        }
        // 如果要根据快递员的手机号来统计快递员派单数量，则不能将录入人手机号定为admin手机号
        // admin不应介入派单业务环节
//        ex.setStaffphone(AdminUtil.getStaffPhone(req.getSession()));
        Message message = new Message();
        boolean insert = false;
        try {
            insert = ExpressService.insertEx(ex);
            if (insert) {
                message.setStatus(0);
            } else {
                message.setStatus(-1);
            }
        } catch (SmsServiceNotAvailableException e) {
            message.setStatus(0);
            message.setResult(e.getMessage());
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/express/search.do")
    public String search(HttpServletRequest req, HttpServletResponse resp){
        String ex_num = req.getParameter("ex_num");
        Express ex = ExpressService.findExByExNum(ex_num);
        Message msg = new Message();
        if (ex == null) {
            msg.setStatus(-1);
            msg.setResult("单号不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(ex);
        }
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        String idStr = req.getParameter("id");
        int id = Integer.parseInt(idStr);
        String ex_num = req.getParameter("ex_num");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userphone = req.getParameter("userphone");
        String statusStr = req.getParameter("status");
        int status = Integer.parseInt(statusStr);
        Express newEx = new Express();
        newEx.setEx_num(ex_num);
        newEx.setUsername(username);
        newEx.setCompany(company);
        newEx.setUserphone(userphone);
        newEx.setStatus(status);
        Message msg = new Message();
        try {
            boolean update = ExpressService.updateEx(id, newEx);
            if (update) {
                msg.setStatus(0);
            } else {
                msg.setStatus(-1);
            }
        } catch (SmsServiceNotAvailableException e) {
            msg.setStatus(0);
            msg.setResult(e.getMessage());
        }
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean delete = ExpressService.deleteEx(id);
        Message msg = new Message();
        if (delete) {
            msg.setStatus(0);
            msg.setResult("删除成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败！");
        }
        return JsonUtil.toJSON(msg);
    }
}
